﻿using LanguageSchool.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LanguageSchool.Controls
{
    /// <summary>
    /// Логика взаимодействия для CalendarGrid.xaml
    /// </summary>
    public partial class CalendarGrid : UserControl
    {
        string[] DaysOfWeek = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };

        private DateTime currentDate;

        public DateTime CurrentDate
        {
            get { return currentDate; }
            set
            {
                currentDate = value;
                InitCells();
            }
        }

        private IEnumerable<Shedule> shedules;
        public IEnumerable<Shedule> Schedules
        {
            set 
            { 
                shedules = value;
                InitCells();
            }
        }

        public CalendarGrid()
        {
            InitializeComponent();
            InitCells();
        }

        private void InitDaysOfWeek()
        {
            grdMain.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
            for (int i = 0; i < 7; i++)
            {
                string dayName = DaysOfWeek[i];
                Label lblDay = new Label();
                lblDay.Content = dayName;
                Grid.SetColumn(lblDay, i + 1);
                Grid.SetRow(lblDay, 0);
                grdMain.Children.Add(lblDay);
            }
        }

        private void InitCells()
        {
            grdMain.RowDefinitions.Clear();
            grdMain.Children.Clear();
            InitDaysOfWeek();
            int firstDay = (int)(new DateTime(CurrentDate.Year, CurrentDate.Month, 1).DayOfWeek);
            firstDay = firstDay == 0 ? 6 : firstDay - 1;
            int dayInMonth = DateTime.DaysInMonth(CurrentDate.Year, CurrentDate.Month);
            int countOfWeek = (int)Math.Ceiling((firstDay + dayInMonth) / 7.0);
            for (int i = 0; i < countOfWeek; i++)
            {
                grdMain.RowDefinitions.Add(new RowDefinition());
            }
            // prev month
            if (firstDay != 0)
            {
                DateTime dt = CurrentDate;
                dt = dt.AddMonths(-1);
                int dayInMonthPrev = DateTime.DaysInMonth(dt.Year, dt.Month);
                int firstDayPrev = dayInMonthPrev - firstDay + 1;
                for (int d = firstDayPrev; d <= dayInMonthPrev; d++)
                {
                    int n = firstDay + d - dayInMonthPrev;
                    int i = (n - 1) / 7 + 1;
                    int j = (n - 1) % 7 + 1;
                    CalendarCell cell = new CalendarCell();
                    cell.Date = new DateTime(dt.Year, dt.Month, d);
                    Grid.SetColumn(cell, j);
                    Grid.SetRow(cell, i);
                    grdMain.Children.Add(cell);
                }
            }
            Random rnd = new Random();
            // current month
            for (int d = 1; d <= dayInMonth; d++)
            {
                int n = firstDay + d;
                int i = (n - 1) / 7 + 1;
                int j = (n - 1) % 7 + 1;
                CalendarCell cell = new CalendarCell();
                DateTime cellDate = new DateTime(currentDate.Year, currentDate.Month, d);
                cell.Date = cellDate;
                // add schedules for this date
                if (shedules != null)
                {
                    var cellSchedules = shedules.Where(s => s.LessonDate != null && s.LessonDate.Value.Date.Equals(cellDate));
                    if (cellSchedules != null && cellSchedules.Any())
                    {
                        cell.CellItems = cellSchedules.ToList();
                    }
                }
                Grid.SetColumn(cell, j);
                Grid.SetRow(cell, i);
                grdMain.Children.Add(cell);
            }
            // next month
            int lastDay = (int)(new DateTime(CurrentDate.Year, CurrentDate.Month, dayInMonth).DayOfWeek);
            lastDay = lastDay == 0 ? 6 : lastDay - 1;
            if (lastDay != 6)
            {
                DateTime dt = CurrentDate;
                dt = dt.AddMonths(1);
                for (int d = 1; d <= 7 - lastDay - 1; d++)
                {
                    int n = firstDay + dayInMonth + d;
                    int i = (n - 1) / 7 + 1;
                    int j = (n - 1) % 7 + 1;
                    CalendarCell cell = new CalendarCell();
                    cell.Date = new DateTime(dt.Year, dt.Month, d);
                    Grid.SetColumn(cell, j);
                    Grid.SetRow(cell, i);
                    grdMain.Children.Add(cell);
                }
            }
        }
    }
}
